<?php

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

require_once( dirname( strtr(__FILE__, '\\', '/') ).'/include/common.inc.php' );

$model = get_post( 'm' ); $model = $model ? strtolower( $model ) : 'home';

$action = get_post( 'a' ); $action = $action ? strtolower( $action ) : 'index';

if( $model == 'uploads' )
{
	//获取用户信息
	if( isset( $slogin['admin'] ) && $slogin['admin'] )
	{
		$watermark = get_post( 'watermark' ); $watermark = empty( $watermark )?0:1;
		$thumb = get_post( 'thumb' ); $thumb = empty( $thumb )?0:1;
		$file_save_name = get_post( 'file_save_name' );
		$thumb_width = get_post( 'imgWigth' ); $thumb_width = empty( $thumb_width )?$SUNCMS['default_thumb_width']:$thumb_width;
		$thumb_height = get_post( 'imgHeight' ); $thumb_height = empty( $thumb_height )?$SUNCMS['default_thumb_height']:$thumb_height;
		$save_path = ( $action == 'thumb' || ( $action == 'images' && ( $watermark || $thumb ) ) )?'temp':$action;

		/**上传**/
		$result = uploads( $save_path );

		if( !empty( $result['error'] ) )
		{
			echo '<html>';
			echo '<head>';
			echo '<title>error</title>';
			echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
			echo '</head>';
			echo '<body>';
			echo '<script type="text/javascript">parent.dialog.alert("'.$result['error'].'");history.back();</script>';
			echo '</body>';
			echo '</html>';
			exit;
		}
		else
		{
			echo '<html>';
			echo '<head>';
			echo '<title>Insert Image</title>';
			echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
			echo '</head>';
			echo '<body>';
			if( $action == 'thumb' )
			{
				$frame = get_post( "frame" );
				echo '<script type="text/javascript">parent.window.frames['.$frame.'].$("#thumb_pic").val("'. $result['file_name'] .'");parent.dialog.dialogClose(1);</script>';
			}
			elseif( $action == 'images' )
			{
				echo '<script type="text/javascript">parent.KE.plugin["image"].insert("' . get_post('id') . '", "' . $result['file_name'] . '","' . get_post('imgTitle') . '","' . get_post('imgWidth') . '","' . get_post('imgHeight') . '","' . get_post('imgBorder') . '", "' . get_post('imgAlign').'");</script>';
			}
			elseif( $action == 'media' || $action == 'flash' )
			{
				echo '<script type="text/javascript">parent.KE.plugin["'.$action.'"].insert("' . get_post('id') . '", "' . $result['file_name'] . '", "' . get_post('imgWidth') . '","' . get_post('imgHeight') . '", "' . get_post('autoStart') . '", "' . get_post('loop').'");</script>';
			}
			elseif( $action == 'enclosure' )
			{
				echo '<script type="text/javascript">parent.KE.plugin["rar"].insert("' . get_post('id') . '", "' . $result['file_name']  . '", "' . get_post('imgTitle'). '");</script>';
			}
			echo '</body>';
			echo '</html>';
		}
	}
	else
	{
		echo '<html>';
		echo '<head>';
		echo '<title>error</title>';
		echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
		echo '</head>';
		echo '<body>';
		echo '<script type="text/javascript">parent.dialog.alert("您所在用户组不允许上传！");history.back();</script>';
		echo '</body>';
		echo '</html>';
	}
}
elseif( $model == 'list' )
{
	include( SUNCMS.'admin/model/filemag.class.php' );
	$m_filemag = new Filemag();
	
	if( $action == 'images' )
	{
		$type = get_post( 'type' ); $type = empty( $type )?'images':$type;
		$page = get_post( 'page' ); $page = empty( $page )?1:$page;
		$images_no = $m_filemag->get_file_no( 'images', $type );
		$page_config = array
		(
				'base_url' => 'editor.php?m=list&a=images&type='.$type,
				'cur_page' => $page,
				'num_links' => 1,
				'num_pages' => ceil( $images_no/10 )
		);
		$pagination = new Pagination( $page_config );
		$page_link = $pagination->create_links();
		$images = $m_filemag->get_file_list( ( $page-1 )*10, 'images', $type );
		include display( 'images', 'editor' );
		unset( $type, $page, $images_no, $page_config, $pagination, $page_link, $images );
	}
	elseif( $action == 'flash' || $action == 'enclosure' || $action == 'media' )
	{
		$page = get_post( 'page' ); $page = empty( $page )?1:$page;
		$file_no = $m_filemag->get_file_no( $action );
		$page_config = array
		(
				'base_url' => 'editor.php?m=list&a='.$action,
				'cur_page' => $page,
				'num_pages' => ceil( $file_no/8 )
		);
		$pagination = new Pagination( $page_config );
		$page_link = $pagination->create_links();
		$files = $m_filemag->get_file_list( ( $page-1 )*8, $action );
		include display( $action, 'editor' );
		unset( $type, $page, $images_no, $page_config, $pagination, $page_link, $images );
	}
	elseif( $action == 'emoticons' )
	{
		$cat = get_post( 'cat' ); $cat = empty( $cat )?'default':$cat;
		$category = $m_filemag->get_emoticons_category();
		$emoticons = $m_filemag->get_emoticons_list( $cat );
		include display( 'emoticons', 'editor' );
		unset( $cat, $page, $emoticons );
	}
	unset( $m_filemag );
}
elseif( $model == 'data' )
{
	include( SUNCMS.'include/editor.class.php' );
	$m_editor = new Editor();
	
	if( $action == 'savedata' )
	{
		$datas = get_post( 'datas' );
		$datas = json_decode( new_stripslashes( $datas ) );
		echo $m_editor->save_data( $datas )?json_encode( array( 'ok' => 1, 'time' => date( 'Y-m-d H:i:s', TIME ) ) ):json_encode( array( 'ok' => 0 ) );
	}
	elseif( $action == 'getlist' )
	{
		$datas = $m_editor->get_list();
		echo json_encode( $datas );
	}
	elseif( $action == 'getdata' )
	{
		$id = get_post( 'id' );
		$datas = $m_editor->get_data( $id );
		echo json_encode( $datas );
	}
}